﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using ZeroBug.EasyFlow.Core;

namespace EasyFlow
{
    public partial class VariableManagerViewModel : ObservableObject
    {
        private FlowManager _flowManager;
        //[ObservableProperty]
        //ObservableCollection<VariableViewModel> _resourceVariables;
        //[ObservableProperty]
        //ObservableCollection<VariableViewModel> _flowVariables;
        //[ObservableProperty]
        //ObservableCollection<VariableViewModel> _recipeVariables;
        //[ObservableProperty]
        //ObservableCollection<VariableViewModel> _globalVariables;
        [ObservableProperty]
        string _selectedVariableName;
        [ObservableProperty]
        ObservableCollection<VariableGroup> _variableGroups;
        List<VariableViewModel2> currentSelectedVariables;
        [ObservableProperty]
        ICollectionView _variableCollectionView;
        [ObservableProperty]
        string _variableFilterText;
        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(VariableOperateCommand))]
        bool canOperateVariable;
        public VariableManagerViewModel()
        {
            VariableGroups = new ObservableCollection<VariableGroup>();
        }
        List<VariableViewModel2> _flowVariables;
        List<VariableViewModel2> _resourceVariables;
        List<VariableViewModel2> _recipeVariables;
        List<VariableViewModel2> _globalVariables;
        List<string> _flowNames;
        public VariableManagerViewModel(FlowManager flowManager)
            : this()
        {
            this._flowManager = flowManager;

            currentSelectedVariables = new List<VariableViewModel2>();
            _flowNames = new List<string>(_flowManager.Flows.Keys);

            this._flowVariables = new List<VariableViewModel2>();
            foreach (var item in _flowManager.Flows)
            {
                this._flowVariables.AddRange(item.Value.Variables.Select(v => new VariableViewModel2(v.Value, _flowManager.Flows.Keys.ToList(), true)));
            }
            VariableGroups.Add(new VariableGroup() { GroupIcon = "\xe786", GroupName = "流程变量" });

            _resourceVariables = this._flowManager.VariableManager.ResourceVariables.Select(v => new VariableViewModel2(v.Value, _flowNames)).ToList();
            VariableGroups.Add(new VariableGroup() { GroupIcon = "\xef1f", GroupName = "资源变量" });

            _recipeVariables = this._flowManager.CurrentRecipe.Variables.Select(v => new VariableViewModel2(v.Value, _flowNames)).ToList();
            VariableGroups.Add(new VariableGroup() { GroupIcon = "\xe678", GroupName = "配方变量" });

            _globalVariables = this._flowManager.VariableManager.GlobalVariables.Select(v => new VariableViewModel2(v.Value, _flowNames)).ToList();
            VariableGroups.Add(new VariableGroup() { GroupIcon = "\xe632", GroupName = "全局变量" });
        }
        public VariableViewModel CreateVariableViewModel(List<Variable> resourceVars, string variableType)
        {
            VariableViewModel axisVarVM = new VariableViewModel(new Variable(variableType)) { IsNodeOnly = true };
            axisVarVM.SetChildren(resourceVars.Where(v => v.Group.Equals(variableType)).ToList());
            foreach (var item in axisVarVM.Children)
            {
                item.SetChildren(resourceVars);
            }
            return axisVarVM;
        }
        string currentSelectedVarTab = "";
        public void ChangeGroupSelection(VariableGroup variableGroup)
        {
            currentSelectedVarTab = variableGroup.GroupName;
            switch (variableGroup.GroupName)
            {
                case "流程变量":
                    CanOperateVariable = true;
                    this.currentSelectedVariables = new List<VariableViewModel2>(_flowVariables);
                    break;
                case "资源变量":
                    CanOperateVariable = false;
                    this.currentSelectedVariables = new List<VariableViewModel2>(_resourceVariables);
                    break;
                case "配方变量":
                    CanOperateVariable = true;
                    this.currentSelectedVariables = new List<VariableViewModel2>(_recipeVariables);
                    break;
                case "全局变量":
                    CanOperateVariable = true;
                    this.currentSelectedVariables = new List<VariableViewModel2>(_globalVariables);
                    break;
            }
            VariableCollectionView = CollectionViewSource.GetDefaultView(this.currentSelectedVariables);
            VariableCollectionView.Filter = (item) =>
            {
                if (string.IsNullOrEmpty(VariableFilterText)) return true;
                var v = item as VariableViewModel2;
                return v.Name.Contains(VariableFilterText);
            };
        }
        partial void OnVariableFilterTextChanged(string? oldValue, string newValue)
        {
            VariableCollectionView.Refresh();
        }

        string createNewVarName(List<VariableViewModel2> vars)
        {
            for (int i = 0; i < 999; i++)
            {
                if (!vars.Any(v => v.Name.Equals($"var{i}")))
                    return $"var{i}";
            }
            return "";
        }

        [RelayCommand(CanExecute = nameof(CanOperateVariable))]
        void VariableOperate(object obj)
        {
            switch (obj.ToString())
            {
                case "add":
                    Variable newVar = null;
                    VariableViewModel2 vm = null;
                    switch (currentSelectedVarTab)
                    {
                        case "流程变量":
                            newVar = new Variable(createNewVarName(_flowVariables));
                            vm = new VariableViewModel2(newVar, _flowNames);
                            vm.IsEdit = true;
                            _flowVariables.Add(vm);
                            break;
                        case "配方变量":
                            newVar = new Variable(createNewVarName(_recipeVariables));
                            vm = new VariableViewModel2(newVar, _flowNames);
                            vm.IsEdit = true;
                            _recipeVariables.Add(vm);
                            break;
                        case "全局变量":
                            newVar = new Variable(createNewVarName(_globalVariables));
                            vm = new VariableViewModel2(newVar, _flowNames);
                            vm.IsEdit = true;
                            _globalVariables.Add(vm);
                            break;
                    }
                    currentSelectedVariables.Add(vm);
                    VariableCollectionView.Refresh();
                    break;
                case "delete":
                    break;
                case "import":
                    break;
                case "export":
                    break;
            }
        }
    }
    public class VariableGroup
    {
        public string GroupIcon { get; set; }
        public string GroupName { get; set; }
    }
}
